package com.example.demo;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class DemoController {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    //没有办理人
    @GetMapping("cs1")
    public void cs1() {
        //根据bpmn文件部署流程
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process1.bpmn").deploy();
        //获取流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
        //启动流程定义，返回流程实例
        ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId());
        String processId = pi.getId();
        System.out.println("流程创建成功，当前流程实例ID:" + processId);

        Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("执行前，任务名称：" + task.getName());
        taskService.complete(task.getId());

        task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
        System.out.println("task为null，任务执行完毕：" + task);
    }

    //单个办理人
    @GetMapping("cs2")
    public void cs2() {
        //根据bpmn文件部署流程
        repositoryService.createDeployment().addClasspathResource("process2.bpmn").deploy();
        //设置User Task1受理人变量
        Map<String, Object> variables = new HashMap<>();
        variables.put("user1", "007");
        //采用key来启动流程定义并设置流程变量，返回流程实例
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("process2", variables);
        String processId = pi.getId();
        System.out.println("流程创建成功，当前流程实例ID:" + processId);

        //注意 这里需要拿007来查询，key-value需要拿value来获取任务
        List<Task> list = taskService.createTaskQuery().taskAssignee("007").list();
        if (list != null && list.size() > 0) {
            for (Task task : list) {
                System.out.println("任务ID：" + task.getId());
                System.out.println("任务办理人：" + task.getAssignee());
                System.out.println("任务名称：" + task.getName());
                System.out.println("任务的创建时间：" + task.getCreateTime());
                System.out.println("流程实例ID：" + task.getProcessInstanceId());
            }
        }
        taskService.complete(list.get(0).getId());
        System.out.println("User Task1被完成了");
    }

    //多个办理人
    @GetMapping("cs3")
    public void cs3() {
        //根据bpmn文件部署流程
        repositoryService.createDeployment().addClasspathResource("process3.bpmn").deploy();
        //设置多个处理人变量 这里设置了三个人
        Map<String, Object> variables = new HashMap<>();
        List<String> userList = new ArrayList<>();
        userList.add("user1");
        userList.add("user2");
        userList.add("user3");
        variables.put("userList", userList);
        variables.put("name","小帅");
        //采用key来启动流程定义并设置流程变量，返回流程实例
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("process3", variables);
        String processId = pi.getId();
        System.out.println("流程创建成功，当前流程实例ID：" + processId);

        //查看user1的任务
        List<Task> list = taskService.createTaskQuery().taskAssignee("user1").list();
        if (list != null && list.size() > 0) {
            for (Task task : list) {
                System.out.println("任务ID：" + task.getId());
                System.out.println("任务的办理人：" + task.getAssignee());
                System.out.println("任务名称：" + task.getName());
                System.out.println("任务的创建时间：" + task.getCreateTime());
                System.out.println("####################################");
                String name = (String) runtimeService.getVariable(task.getExecutionId(), "name");
                System.out.println(name);
                taskService.complete(task.getId());
            }
        }
        List<Task> taskList = taskService.createTaskQuery().processInstanceId(processId).list();

        //查看user2的任务
        List<Task> list2 = taskService.createTaskQuery().taskAssignee("user2").list();
        if (list2 != null && list2.size() > 0) {
            for (Task task : list2) {
                System.out.println("任务ID：" + task.getId());
                System.out.println("任务的办理人：" + task.getAssignee());
                System.out.println("任务名称：" + task.getName());
                System.out.println("任务的创建时间：" + task.getCreateTime());
                System.out.println("####################################");
                taskService.complete(task.getId());
            }
        }
        List<Task> taskList2 = taskService.createTaskQuery().processInstanceId(processId).list();

        //查看user1的任务
        List<Task> list3 = taskService.createTaskQuery().taskAssignee("user3").list();
        if (list3 != null && list3.size() > 0) {
            for (Task task : list3) {
                System.out.println("任务ID：" + task.getId());
                System.out.println("任务的办理人：" + task.getAssignee());
                System.out.println("任务名称：" + task.getName());
                System.out.println("任务的创建时间：" + task.getCreateTime());
                System.out.println("####################################");
                taskService.complete(task.getId());
            }
        }
        List<Task> taskList3 = taskService.createTaskQuery().processInstanceId(processId).list();
    }

    //候选人
    @GetMapping("cs4")
    public void cs4(){
        repositoryService.createDeployment().addClasspathResource("process4.bpmn").deploy();
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("process4");
        String processId=pi.getId();
        System.out.println("流程创建成功，当前流程实例ID:"+processId);

        List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("xiaoshuai").list();
        for (Task task:list){
            System.out.println("任务ID：" + task.getId());
            System.out.println("任务的办理人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
            System.out.println("任务的创建时间：" + task.getCreateTime());
            System.out.println("####################################");
//            taskService.complete(task.getId());
        }

    }
    //根据候选人获取任务
    @GetMapping("cs5")
    public void cs5(){
        List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("xiaoshuai").list();
        System.out.println(list);
    }
    //候选人xiaoshuai拾取任务
    @GetMapping("cs6")
    public void cs6(){
        taskService.claim("10009","xiaoshuai");
    }
    //候选dashuai拾取任务（小帅拾取任务之后），会抛出一个异常Task '10009' is already claimed by someone else
    @GetMapping("cs7")
    public void cs7(){
        taskService.claim("10009","dashuai");
    }
    //一个任务多次被执行，会抛出异常Cannot find task with id 10009
    @GetMapping("cs8")
    public void cs8(){
        taskService.complete("10009");
    }

    //排他网关的使用1
    @GetMapping("pt1")
    public void pt1(){
        repositoryService.createDeployment().addClasspathResource("process5.bpmn").deploy();
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("process5");
        String processId=pi.getId();
        System.out.println("流程创建成功，当前流程实例ID:"+processId);
    }
    //排他网关的使用2
    @GetMapping("pt2")
    public void pt2(){
        //小帅拾取任务
        List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("小帅").list();
        for (Task task:list){
            taskService.claim(task.getId(),"小帅");
            //小帅把任务打回去资料调整
            Map<String,Object> variables=new HashMap<>();
            variables.put("state","资料调整");
            taskService.complete(task.getId(),variables);
        }
    }
    //排他网关的使用3
    @GetMapping("pt3")
    public void pt3(){
        List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("大帅").list();
        for (Task task:list){
            taskService.claim(task.getId(),"大帅");
            //大帅执行任务
            taskService.complete(task.getId());
        }
    }
    //排他网关的使用4
    @GetMapping("pt4")
    public void pt4() {
        //任务又回到小帅，小帅审核通过，流程结束
        List<Task> list = taskService.createTaskQuery().taskCandidateOrAssigned("小帅").list();
        for (Task task:list){
            //更改判断条件
            Map<String, Object> variables = task.getProcessVariables();
            variables.put("state","通过");
            taskService.claim(task.getId(),"小帅");
            System.out.println("任务ID：" + task.getId());
            System.out.println("任务的办理人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
            System.out.println("任务的创建时间：" + task.getCreateTime());
            System.out.println("####################################");
            taskService.complete(task.getId(),variables);
        }
    }
}
